package com.zhiche.lisa.bms.controller.receipt;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.BMSApplication;
import com.zhiche.lisa.bms.dao.model.receipt.ReceiptAffirm;
import com.zhiche.lisa.bms.dao.model.receipt.ReceiptAffirmDtl;
import com.zhiche.lisa.bms.pojo.vo.receipt.ReceiptAffirmAndDtlVO;
import com.zhiche.lisa.bms.service.utils.UaaUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BMSApplication.class)
public class ReceiptAffirmControllerText {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext context;

    private String token;

    @Autowired
    private UaaUtil uaaUtil;

    @Autowired
    private Filter springSecurityFilterChain;
    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilter(springSecurityFilterChain).build();
//        token = uaaUtil.getTenantToken("15110288389");
        token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnbXRfY3JlYXRlIjoiMTUzODAzODE3OTg3MiIsImFjY291bnRJZCI6MTAyLCJ1c2VyX25hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIiwic2NvcGUiOlsiYWxsIl0sInJvbGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJ0ZW5hbnRJZCI6MSwiY29ycE5hbWUiOiLkuK3ogZTnianmtYHvvIjkuK3lm73vvInmnInpmZDlhazlj7giLCJleHAiOjE1MzgwNDUzNzksImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiN2ZhZjlhOTUtMmRiOC00M2Y5LWFkNGYtZTZmYWZiOGQzNDg2IiwiY2xpZW50X2lkIjoiZGV2b3BzIiwidXNlcm5hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIn0.9TqfcincKclYbJEmhCkHI52__L7LcDaTC65uPbyIMU4";
    }
@Test
    public void qureyReceiptAffirmLineText()throws Exception{
    Page<ReceiptAffirm> page = new Page<>(1,10);
    Map<String,Object> map = Maps.newHashMap();
    map.put("billCode","1");
    map.put("payName","中联物流");
    page.setCondition(map);
    String a = JSONObject.toJSONString(page);
    System.err.println(token);
    RequestBuilder request = MockMvcRequestBuilders.post("/receiptAffirm/qureyReceiptAffirmLinePage")
            .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
            .accept(MediaType.APPLICATION_JSON);
    MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    System.out.println("返回结果状态：" + status);
    System.out.println(content);
    Assert.assertEquals(status,200);
}
@Test
    public void deleteReceiptAffirmById()throws Exception{
    List<Long> list = new ArrayList<>();
//    list.add(1L);
    list.add(2L);
    list.add(3L);
//    list.add(4L);
    String a = JSONObject.toJSONString(list);
    System.err.println(token);
    RequestBuilder request = MockMvcRequestBuilders.post("/receiptAffirm/deleteReceiptAffirmById")
            .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
            .accept(MediaType.APPLICATION_JSON);
    MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    System.out.println("返回结果状态：" + status);
    System.out.println(content);
    Assert.assertEquals(status,200);
}
@Test
public void insertReceiptAffirm() throws Exception {
    ReceiptAffirmAndDtlVO receiptAffirmVO = new ReceiptAffirmAndDtlVO();
    ReceiptAffirm receiptAffirm = new ReceiptAffirm();
    receiptAffirm.setDiffCause("将数据");
    receiptAffirm.setDiffSum(new BigDecimal("3515"));
    receiptAffirm.setGmtCreate(new Date());
    List<ReceiptAffirmDtl> list = new ArrayList<>();
    ReceiptAffirmDtl receiptAffirmDtl = new ReceiptAffirmDtl();
    receiptAffirmDtl.setAccountSum(new BigDecimal("256"));
    receiptAffirmDtl.setInvoiceId(2L);
    receiptAffirmDtl.setUndeteSum(new BigDecimal("4556"));
    list.add(receiptAffirmDtl);
    receiptAffirmVO.setReceiptAffirm(receiptAffirm);
    receiptAffirmVO.setReceiptAffirmDtls(list);
    String a = JSONObject.toJSONString(receiptAffirmVO);
    System.err.println(token);
    RequestBuilder request = MockMvcRequestBuilders.post("/receiptAffirm/insertReceiptAffirm")
            .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
            .accept(MediaType.APPLICATION_JSON);
    MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    System.out.println("返回结果状态：" + status);
    System.out.println(content);
    Assert.assertEquals(status,200);
}
}
